<?php

namespace App\Http\Admin\Controller\Config;

use App\Http\Admin\Middleware\AuthMiddleware;
use App\Http\Common\Controller\AbstractController;
use App\Http\Common\Result;
use App\Request\auth\Service\Config\ConfigService;
use App\Request\config\ConfigRequest;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\DeleteMapping;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Annotation\PutMapping;
use Hyperf\Validation\Annotation\Scene;

#[Middlewares([
    AuthMiddleware::class,
])]
#[Controller(prefix: '/adminapi/config/config/')]
class ConfigController extends AbstractController
{
    public function __construct(
        protected readonly ConfigService $service,

    )
    {
    }

    /**
     * 配置列表
     * @return Result
     */
    #[GetMapping('lists')]
    public function lists(): Result
    {
        return $this->success($this->service->page($this->request->all(),
            $this->getCurrentPage(),
            $this->getPageSize()
        ));
    }



    /**
     * 配置详情
     * @param int $id
     * @return Result
     */
    #[GetMapping("detail/{id}")]
    public function detail(int $id): Result
    {
        return $this->success($this->service->findById($id));
    }

    /**
     * 配置创建
     * @param ConfigRequest $request
     * @return Result
     */
    #[Scene(scene: 'create', argument: 'request')]
    #[PostMapping("create")]
    public function create(ConfigRequest $request): Result
    {
        return $this->success($this->service->create($request->all()));
    }

    /**
     * 配置更新
     * @param ConfigRequest $request
     * @return Result
     */
    #[Scene(scene: 'update', argument: 'request')]
    #[PutMapping("update")]
    public function update(ConfigRequest $request): Result
    {
        return $this->success($this->service->updateById($request->input('id'), $request->all()));
    }

    /**
     * 配置删除
     * @param int $id
     * @return Result
     */
    #[DeleteMapping("delete/{id}")]
    public function delete(int $id): Result
    {
        $this->service->deleteById($id);
        return $this->success();
    }

}